<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" left-arrow @click-left="goBack"/>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        clearable
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji">
          <van-icon name="phone"/>
        </i>
      </van-field>
      <van-field
        v-model="user.code"
        type="number"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma">
          <van-icon name="lock"/>
        </i>
        <template #button>
          <van-button
            class="send-sms-btn"
            round
            size="small"
            type="default"
            :disabled="sending"
            @click="onSendAuthCode"
          >{{sendText}}</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import * as reg from "@/constants/reg";
import * as api from "../../api/user";
import { Notify } from "vant";
import { withMsg } from '../../utils/request';
export default {
  data() {
    return {
      user: {
        mobile: "", // 手机号
        code: "" // 验证码
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空"
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "手机号格式错误"
          }
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空"
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误"
          }
        ]
      },
      sending: false,
      delay: 60,
      sendText: "发送验证码"
    };
  },
  methods: {
    async onSubmit(params) {
      // console.log("submit", params);
      // const res = await api.login(params).catch(() => {
      //   Notify({ type: "danger", message: "登录失败" });
      // });
      // console.log(res);
      // Notify({ type: "success", message: "登录成功" });
      // localStorage.setItem("TOKEN", `Bearer ${res.data.data.token}`);
      // setTimeout(() => {
      //   api.getUserProfile();
      // }, 1000);
      const res = await withMsg( api.login(params))
      console.log(res);
      localStorage.setItem("TOKEN", `Bearer ${res.data.data.token}`);
      this.$router.push("/home");
    },
    onSendAuthCode() {
      this.sending = true;
      const countDown = () => {
        this.delay--;
        this.sendText = this.delay;
      };
      countDown();
      const timer = setInterval(countDown, 1000);
      setTimeout(() => {
        this.sending = false;
        this.sendText = "发送验证码";
        clearTimeout(timer);
      }, 60000);
      // const res = await api.sendAuthCode(this.user.mobile).catch(() => {
      //   Notify({ type: "danger", message: "获取验证码失败" });
      // });
      // console.log(res);
      // Notify({ type: "success", message: "获取验证码成功" });
      withMsg( api.sendAuthCode(this.user.mobile) )
    },
    goBack() {}
  }
};
</script>
<style scoped lang="less">
.login-container {
  .van-form {
    margin-top: 50px;
  }
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 160px;
    height: 50px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>